package com.cmwa.solution.business.myHome.todo.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import com.cmwa.solution.business.infodisclosure.investReport.dao.InvestReportDao;
import com.cmwa.solution.business.infodisclosure.investReport.entity.InvestReportVo;
import com.cmwa.solution.business.myHome.todo.entity.TodoBizRelVo;
import com.cmwa.solution.business.myHome.todo.entity.TodoVo;
import com.cmwa.solution.business.myHome.todo.enums.ServiceType;
import com.cmwa.solution.business.myHome.todo.enums.TodoBizType;
import com.cmwa.solution.business.myHome.todo.service.AbsTodoBizRelGenerator;
import com.cmwa.solution.business.myHome.todo.service.TodoBizRelGenerateFactory;
import com.cmwa.solution.utils.DateUtils;
import com.cmwa.solution.utils.SessionUtils;
import com.google.common.base.Joiner;

@Service
public class InvestReportTodoBizRelGenerate extends AbsTodoBizRelGenerator implements InitializingBean {

	@Resource
	private InvestReportDao investReportDao;
	
	public InvestReportTodoBizRelGenerate() {
		this.setServiceTypes(ServiceType.INFO_DIS_INVESTER);
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		TodoBizRelGenerateFactory.addGenerator(ServiceType.INFO_DIS_INVESTER, this);
	}


	@Override
	public void generateByTodo(TodoVo todoVo) {
		InvestReportVo investReport = new InvestReportVo();
		// 保存估值表业务数据
		String empId = SessionUtils.getEmployee().getID();
		Date todoDate = todoVo.getTodoDate();
		String date = DateUtils.formatDate(todoDate, DateUtils.DATE_FORMAT_yMd_EN);
		
		investReport.setReportType(todoVo.getTodoType());
		investReport.setReportTime(date);
		investReport.setPrjCode(todoVo.getPrjCode());
		investReport.setCreateId(empId);
		investReport.setStatus("S");
		investReportDao.insertInvestReport(investReport);
		
		saveTodoBizRel(buildRelVo(todoVo, TodoBizType.INFO_DIS_INVESTER, investReport.getInvestRepId()));
	}

	@Override
	public void deleteByTodo(TodoVo todoVo) {
		List<TodoBizRelVo> todoBizRels = todoBizRelDao.queryByTodo(TodoBizType.INFO_DIS_INVESTER, todoVo.getId());
		if (CollectionUtils.isNotEmpty(todoBizRels)) {
			List<String> bizIds = new ArrayList<>();
			for (TodoBizRelVo relVo : todoBizRels) {
				if (StringUtils.isNotBlank(relVo.getBizId())) {
					bizIds.add(relVo.getBizId());
				}
			}
			
			if (CollectionUtils.isNotEmpty(bizIds)) {
				investReportDao.logicDeleteInvestReportById(Joiner.on(",").join(bizIds));
			}
		}
	}

}
